package com.todo.reward.infrastructure.convert;

import com.google.api.client.util.Lists;
import com.todo.reward.infrastructure.entity.domain.BaseTemplateDO;
import com.todo.reward.infrastructure.entity.domain.CouponExtensionDO;
import com.todo.reward.infrastructure.entity.domain.GrowthExtensionDO;
import com.todo.reward.infrastructure.entity.domain.PointsExtensionDO;
import com.todo.reward.infrastructure.entity.po.RewardCouponTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardGrowthTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardPointsTemplatePO;
import com.todo.reward.infrastructure.entity.po.RewardTemplatePO;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author azhebuxing
 * @date 2025/3/4 01:51
 * @description
 */
public class RewardTemplateConvert {
    public static <T> RewardTemplatePO buildRewardTemplatePO(BaseTemplateDO<T> baseTemplate) {
        RewardTemplatePO rewardTemplatePO = new RewardTemplatePO();
        rewardTemplatePO.setId(baseTemplate.getId());
        rewardTemplatePO.setRewardType(baseTemplate.getRewardType());
        rewardTemplatePO.setTitle(baseTemplate.getTitle());
        rewardTemplatePO.setExpiredType(baseTemplate.getExpiredType());
        rewardTemplatePO.setExpiredTime(baseTemplate.getExpiredTime());
        rewardTemplatePO.setDispatchType(baseTemplate.getDispatchType());
        rewardTemplatePO.setBusinessLine(baseTemplate.getBusinessLine());
        rewardTemplatePO.setTotalAmount(baseTemplate.getTotalAmount());
        rewardTemplatePO.setRemainAmount(baseTemplate.getRemainAmount());
        rewardTemplatePO.setIsRepeat(baseTemplate.getIsRepeat() ? 1 : 0);
        rewardTemplatePO.setMaxRepeatCount(baseTemplate.getMaxRepeatCount());
        rewardTemplatePO.setDispatchRule(baseTemplate.getDispatchRule());
        rewardTemplatePO.setStatus(baseTemplate.getStatus());
        rewardTemplatePO.setIsDelete(baseTemplate.getIsDelete());
        rewardTemplatePO.setCreateTime(baseTemplate.getCreateTime());
        rewardTemplatePO.setUpdateTime(baseTemplate.getUpdateTime());
        return rewardTemplatePO;
    }

    public static RewardPointsTemplatePO buildRewardPointsTemplatePO(PointsExtensionDO pointsExtensionDO, Long id) {
        RewardPointsTemplatePO rewardPointsTemplatePO = new RewardPointsTemplatePO();
        rewardPointsTemplatePO.setTemplateId(id);
        rewardPointsTemplatePO.setAmountDispatchType(pointsExtensionDO.getAmountDispatchType());
        rewardPointsTemplatePO.setPointsAmount(pointsExtensionDO.getPointsAmount());
        rewardPointsTemplatePO.setAmountCalRule(pointsExtensionDO.getAmountCalRule());
        rewardPointsTemplatePO.setPointsDesc(pointsExtensionDO.getPointsDesc());
        return rewardPointsTemplatePO;
    }

    public static RewardGrowthTemplatePO buildRewardGrowthTemplatePO(GrowthExtensionDO growthExtensionDO, Long id) {
        RewardGrowthTemplatePO rewardGrowthTemplatePO = new RewardGrowthTemplatePO();
        rewardGrowthTemplatePO.setTemplateId(id);
        rewardGrowthTemplatePO.setGrowthAmount(growthExtensionDO.getGrowthAmount());
        rewardGrowthTemplatePO.setGrowthDispatchType(growthExtensionDO.getGrowthDispatchType());
        rewardGrowthTemplatePO.setAmountCalRule(growthExtensionDO.getAmountCalRule());
        rewardGrowthTemplatePO.setGrowthDesc(growthExtensionDO.getGrowthDesc());
        return rewardGrowthTemplatePO;
    }

    public static RewardCouponTemplatePO buildRewardCouponTemplatePO(CouponExtensionDO couponExtensionDO, Long id) {
        RewardCouponTemplatePO rewardCouponTemplatePO = new RewardCouponTemplatePO();
        rewardCouponTemplatePO.setTemplateId(id);
        rewardCouponTemplatePO.setCouponDesc(couponExtensionDO.getCouponDesc());
        rewardCouponTemplatePO.setUseDesc(couponExtensionDO.getUseDesc());
        rewardCouponTemplatePO.setCouponBizType(couponExtensionDO.getCouponBizType());
        rewardCouponTemplatePO.setCouponType(couponExtensionDO.getCouponType());
        rewardCouponTemplatePO.setCouponAmount(couponExtensionDO.getCouponAmount());
        rewardCouponTemplatePO.setAmountDispatchType(couponExtensionDO.getAmountDispatchType());
        rewardCouponTemplatePO.setAmountCalRule(couponExtensionDO.getAmountCalRule());
        rewardCouponTemplatePO.setUseRule(couponExtensionDO.getUseRule());
        rewardCouponTemplatePO.setCouponExpireType(couponExtensionDO.getCouponExpireType());
        rewardCouponTemplatePO.setCouponExpireDays(couponExtensionDO.getCouponExpireDays());
        return rewardCouponTemplatePO;
    }

    public static <T> BaseTemplateDO<PointsExtensionDO> buildBaseTemplateDO(RewardPointsTemplatePO rewardPointsTemplatePO, RewardTemplatePO rewardTemplatePO) {
        BaseTemplateDO<PointsExtensionDO> baseTemplate = new BaseTemplateDO<>();
        baseTemplate.setId(rewardTemplatePO.getId());
        baseTemplate.setRewardType(rewardTemplatePO.getRewardType());
        baseTemplate.setTitle(rewardTemplatePO.getTitle());
        baseTemplate.setExpiredType(rewardTemplatePO.getExpiredType());
        baseTemplate.setExpiredTime(rewardTemplatePO.getExpiredTime());
        baseTemplate.setDispatchType(rewardTemplatePO.getDispatchType());
        baseTemplate.setBusinessLine(rewardTemplatePO.getBusinessLine());
        baseTemplate.setTotalAmount(rewardTemplatePO.getTotalAmount());
        baseTemplate.setRemainAmount(rewardTemplatePO.getRemainAmount());
        baseTemplate.setIsRepeat(rewardTemplatePO.getIsRepeat() == 1);
        baseTemplate.setMaxRepeatCount(rewardTemplatePO.getMaxRepeatCount());
        baseTemplate.setDispatchRule(rewardTemplatePO.getDispatchRule());
        baseTemplate.setStatus(rewardTemplatePO.getStatus());
        baseTemplate.setIsDelete(rewardTemplatePO.getIsDelete());
        baseTemplate.setCreateTime(rewardTemplatePO.getCreateTime());
        baseTemplate.setUpdateTime(rewardTemplatePO.getUpdateTime());
        PointsExtensionDO pointsExtensionDO = new PointsExtensionDO();
        pointsExtensionDO.setAmountDispatchType(rewardPointsTemplatePO.getAmountDispatchType());
        pointsExtensionDO.setPointsAmount(rewardPointsTemplatePO.getPointsAmount());
        pointsExtensionDO.setAmountCalRule(rewardPointsTemplatePO.getAmountCalRule());
        pointsExtensionDO.setPointsDesc(rewardPointsTemplatePO.getPointsDesc());
        baseTemplate.setExtension(pointsExtensionDO);
        return baseTemplate;
    }

    public static <T> BaseTemplateDO<GrowthExtensionDO> buildBaseTemplateDO(RewardGrowthTemplatePO rewardGrowthTemplatePO, RewardTemplatePO rewardTemplatePO) {
        BaseTemplateDO<GrowthExtensionDO> baseTemplate = new BaseTemplateDO<>();
        baseTemplate.setId(rewardTemplatePO.getId());
        baseTemplate.setRewardType(rewardTemplatePO.getRewardType());
        baseTemplate.setTitle(rewardTemplatePO.getTitle());
        baseTemplate.setExpiredType(rewardTemplatePO.getExpiredType());
        baseTemplate.setExpiredTime(rewardTemplatePO.getExpiredTime());
        baseTemplate.setDispatchType(rewardTemplatePO.getDispatchType());
        baseTemplate.setBusinessLine(rewardTemplatePO.getBusinessLine());
        baseTemplate.setTotalAmount(rewardTemplatePO.getTotalAmount());
        baseTemplate.setRemainAmount(rewardTemplatePO.getRemainAmount());
        baseTemplate.setIsRepeat(rewardTemplatePO.getIsRepeat() == 1);
        baseTemplate.setMaxRepeatCount(rewardTemplatePO.getMaxRepeatCount());
        baseTemplate.setDispatchRule(rewardTemplatePO.getDispatchRule());
        baseTemplate.setStatus(rewardTemplatePO.getStatus());
        baseTemplate.setIsDelete(rewardTemplatePO.getIsDelete());
        baseTemplate.setCreateTime(rewardTemplatePO.getCreateTime());
        baseTemplate.setUpdateTime(rewardTemplatePO.getUpdateTime());
        GrowthExtensionDO growthExtensionDO = new GrowthExtensionDO();
        growthExtensionDO.setGrowthAmount(rewardGrowthTemplatePO.getGrowthAmount());
        growthExtensionDO.setGrowthDispatchType(rewardGrowthTemplatePO.getGrowthDispatchType());
        growthExtensionDO.setAmountCalRule(rewardGrowthTemplatePO.getAmountCalRule());
        growthExtensionDO.setGrowthDesc(rewardGrowthTemplatePO.getGrowthDesc());
        baseTemplate.setExtension(growthExtensionDO);
        return baseTemplate;
    }

    public static <T> BaseTemplateDO<CouponExtensionDO> buildBaseTemplateDO(RewardCouponTemplatePO rewardGrowthTemplatePO, RewardTemplatePO rewardTemplatePO) {
        BaseTemplateDO<CouponExtensionDO> baseTemplate = new BaseTemplateDO<>();
        baseTemplate.setId(rewardTemplatePO.getId());
        baseTemplate.setRewardType(rewardTemplatePO.getRewardType());
        baseTemplate.setTitle(rewardTemplatePO.getTitle());
        baseTemplate.setExpiredType(rewardTemplatePO.getExpiredType());
        baseTemplate.setExpiredTime(rewardTemplatePO.getExpiredTime());
        baseTemplate.setDispatchType(rewardTemplatePO.getDispatchType());
        baseTemplate.setBusinessLine(rewardTemplatePO.getBusinessLine());
        baseTemplate.setTotalAmount(rewardTemplatePO.getTotalAmount());
        baseTemplate.setRemainAmount(rewardTemplatePO.getRemainAmount());
        baseTemplate.setIsRepeat(rewardTemplatePO.getIsRepeat() == 1);
        baseTemplate.setMaxRepeatCount(rewardTemplatePO.getMaxRepeatCount());
        baseTemplate.setDispatchRule(rewardTemplatePO.getDispatchRule());
        baseTemplate.setStatus(rewardTemplatePO.getStatus());
        baseTemplate.setIsDelete(rewardTemplatePO.getIsDelete());
        baseTemplate.setCreateTime(rewardTemplatePO.getCreateTime());
        baseTemplate.setUpdateTime(rewardTemplatePO.getUpdateTime());
        CouponExtensionDO couponExtensionDO = new CouponExtensionDO();
        couponExtensionDO.setCouponDesc(rewardGrowthTemplatePO.getCouponDesc());
        couponExtensionDO.setUseDesc(rewardGrowthTemplatePO.getUseDesc());
        couponExtensionDO.setCouponBizType(rewardGrowthTemplatePO.getCouponBizType());
        couponExtensionDO.setCouponType(rewardGrowthTemplatePO.getCouponType());
        couponExtensionDO.setCouponAmount(rewardGrowthTemplatePO.getCouponAmount());
        couponExtensionDO.setAmountDispatchType(rewardGrowthTemplatePO.getAmountDispatchType());
        couponExtensionDO.setAmountCalRule(rewardGrowthTemplatePO.getAmountCalRule());
        couponExtensionDO.setUseRule(rewardGrowthTemplatePO.getUseRule());
        couponExtensionDO.setCouponExpireType(rewardGrowthTemplatePO.getCouponExpireType());
        couponExtensionDO.setCouponExpireDays(rewardGrowthTemplatePO.getCouponExpireDays());
        baseTemplate.setExtension(couponExtensionDO);
        return baseTemplate;
    }

    public static List<BaseTemplateDO<PointsExtensionDO>> buildPointBaseTemplateDOList(List<RewardPointsTemplatePO> rewardPointsTemplatePOList, List<RewardTemplatePO> rewardTemplatePOS) {
        if (CollectionUtils.isEmpty(rewardPointsTemplatePOList) || CollectionUtils.isEmpty(rewardTemplatePOS)){
            return Lists.newArrayList();
        }
        Map<Long, RewardPointsTemplatePO> pointsTemplatePOMap = rewardPointsTemplatePOList.stream().collect(Collectors.toMap(RewardPointsTemplatePO::getTemplateId, Function.identity()));
        List<BaseTemplateDO<PointsExtensionDO>> baseTemplateDOList = Lists.newArrayList();
        for (RewardTemplatePO rewardTemplatePO : rewardTemplatePOS){
            if (pointsTemplatePOMap.containsKey(rewardTemplatePO.getId())){
                BaseTemplateDO<PointsExtensionDO> pointsExtensionDOBaseTemplateDO = buildBaseTemplateDO(pointsTemplatePOMap.get(rewardTemplatePO.getId()), rewardTemplatePO);
                baseTemplateDOList.add(pointsExtensionDOBaseTemplateDO);
            }
        }
        return baseTemplateDOList;
    }

    public static List<BaseTemplateDO<GrowthExtensionDO>> buildGrowthBaseTemplateDOList(List<RewardGrowthTemplatePO> rewardGrowthTemplatePOList, List<RewardTemplatePO> rewardTemplatePOS) {
        if (CollectionUtils.isEmpty(rewardGrowthTemplatePOList) || CollectionUtils.isEmpty(rewardTemplatePOS)){
            return Lists.newArrayList();
        }
        Map<Long, RewardGrowthTemplatePO> pointsTemplatePOMap = rewardGrowthTemplatePOList.stream().collect(Collectors.toMap(RewardGrowthTemplatePO::getTemplateId, Function.identity()));
        List<BaseTemplateDO<GrowthExtensionDO>> baseTemplateDOList = Lists.newArrayList();
        for (RewardTemplatePO rewardTemplatePO : rewardTemplatePOS){
            if (pointsTemplatePOMap.containsKey(rewardTemplatePO.getId())){
                BaseTemplateDO<GrowthExtensionDO> pointsExtensionDOBaseTemplateDO = buildBaseTemplateDO(pointsTemplatePOMap.get(rewardTemplatePO.getId()), rewardTemplatePO);
                baseTemplateDOList.add(pointsExtensionDOBaseTemplateDO);
            }
        }
        return baseTemplateDOList;
    }

    public static List<BaseTemplateDO<CouponExtensionDO>> buildCouponBaseTemplateDOList(List<RewardCouponTemplatePO> rewardCouponTemplatePOList, List<RewardTemplatePO> rewardTemplatePOS) {
        if (CollectionUtils.isEmpty(rewardCouponTemplatePOList) || CollectionUtils.isEmpty(rewardTemplatePOS)){
            return Lists.newArrayList();
        }
        Map<Long, RewardCouponTemplatePO> pointsTemplatePOMap = rewardCouponTemplatePOList.stream().collect(Collectors.toMap(RewardCouponTemplatePO::getTemplateId, Function.identity()));
        List<BaseTemplateDO<CouponExtensionDO>> baseTemplateDOList = Lists.newArrayList();
        for (RewardTemplatePO rewardTemplatePO : rewardTemplatePOS){
            if (pointsTemplatePOMap.containsKey(rewardTemplatePO.getId())){
                BaseTemplateDO<CouponExtensionDO> pointsExtensionDOBaseTemplateDO = buildBaseTemplateDO(pointsTemplatePOMap.get(rewardTemplatePO.getId()), rewardTemplatePO);
                baseTemplateDOList.add(pointsExtensionDOBaseTemplateDO);
            }
        }
        return baseTemplateDOList;
    }
}
